<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Repositories\OthersRepository;
use Illuminate\Support\Facades\Redis;

class OthersController extends Controller
{
    private $other = null;

    public function __construct(OthersRepository $other)
    {
        $this->other = $other;
    }

    /**
     * 获取首页数据
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getIndexData(Request $request)
    {
        $ret = $this->other->getIndexData((int)$request->get("id"));
        return $ret ? parent::jsonReturn($ret, parent::CODE_SUCCESS, "success") :
            parent::jsonReturn([], parent::CODE_FAIL, "data error");
    }

    /**
     * 获取首页实时资讯
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getIndexInformations(Request $request)
    {
        $pageSize = (int)($request->get("pageSize") ?: 15);
        $page = max($request->get("page"), 1);
        $start = ($page - 1) * $pageSize;
        $end = $start + $pageSize - 1;
        $data = Redis::lrange(INDEX_INFORMATION_STORAGE_NAME, $start, $end);
        foreach ($data as $k => $v) {
            $data[$k] = unserialize($v);
        }
        return parent::jsonReturn($data, parent::CODE_SUCCESS, "success");
    }

    /**
     * 获取首页大盘点评
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getIndexComments(Request $request)
    {
        $pageSize = (int)($request->get("pageSize") ?: 15);
        $page = max($request->get("page"), 1);
        $start = ($page - 1) * $pageSize;
        $end = $start + $pageSize - 1;
        $data = Redis::lrange(INDEX_COMMENTS_STORAGE_NAME, $start, $end);
        foreach ($data as $k => $v) {
            $data[$k] = unserialize($v);
        }
        return parent::jsonReturn($data, parent::CODE_SUCCESS, "success");
    }
}